package com.zengdada.authority.service.impl;

import com.zengdada.authority.config.security.intercept.AuthorityUtil;
import com.zengdada.authority.mapper.AppPowerMapper;
import com.zengdada.authority.mapper.AppRoleMapper;
import com.zengdada.authority.model.AppPower;
import com.zengdada.authority.model.AppRole;
import com.zengdada.authority.model.AppUser;
import com.zengdada.authority.service.AppRoleAuthorityService;
import com.zengdada.authority.service.EnhanceBaseService;
import com.zengdada.authority.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.weekend.Fn;
import tk.mybatis.mapper.weekend.WeekendSqls;
import tk.mybatis.mapper.weekend.reflection.Reflections;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AppRoleAuthorityServiceImpl extends EnhanceBaseService<AppRole> implements AppRoleAuthorityService {


    @Autowired
    AppPowerMapper appPowerMapper;

    @Autowired
    AuthorityUtil authorityUtil;

    public static final String TYPE_ROLE = "101";
    public static final String TYPE_DEPARTMENT = "102";

    @Value("${authority.application.id}")
    private String APPLICATION_ID;

    @Override
    public List<Map<String, Object>> getRoleByUser(AppUser user) {
        return CommonUtils.depthTransformHumpNew(((AppRoleMapper) mapper).selectRoleByUserId(user.getId()));
    }

    @Override
    public List<Map<String, Object>> selectAllResource(String appId) {
        return CommonUtils.depthTransformHumpNew(appPowerMapper.selectAllResource(appId, TYPE_ROLE));
    }

    @Override
    public List<AppPower> selectAllNotResource(String appId) {
        return appPowerMapper.selectByExample(new Example.Builder(AppPower.class).select(Reflections.fnToFieldName((Fn<AppPower, Object>) AppPower::getUrl))
                .where(WeekendSqls.<AppPower>custom().andEqualTo(AppPower::getIntercept, 0).andIsNotNull(AppPower::getUrl)
                        .andNotEqualTo(AppPower::getState, 0).andIn(AppPower::getApplicationId, Arrays.asList("0", appId))).build());
    }


    @Override
    public List<Map<String, Object>> getRoleOrDepartment(List<String> types) {
        AppUser user = authorityUtil.getLoginUser();
        boolean isSuperAdministrator = authorityUtil.isSuperAdministrator();
        List<Map<String, Object>> list = CommonUtils.depthTransformHumpNew(((AppRoleMapper) mapper).selectRoleAndSon(user.getId(), isSuperAdministrator, APPLICATION_ID, types));
        return list.stream().filter(e -> "1".equals(String.valueOf(e.get("state")))).collect(Collectors.toList());
    }

    @Override
    public List<Map<String, Object>> getRoleOrDepartmentTree(List<String> types) {
        AppUser user = authorityUtil.getLoginUser();
        boolean isSuperAdministrator = authorityUtil.isSuperAdministrator();
        List<Map<String, Object>> list = CommonUtils.depthTransformHumpNew(((AppRoleMapper) mapper).selectRoleAndSon(user.getId(), isSuperAdministrator, APPLICATION_ID, types));
//        List<Map<String, Object>> collect = list.stream().filter(e -> "1".equals(String.valueOf(e.get("state"))) && (e.get("parentId") != null && !StringUtils.isBlank(String.valueOf(e.get("parentId"))))).collect(Collectors.toList());
        List<Map<String, Object>> collect = list.stream().filter(e -> "1".equals(String.valueOf(e.get("state")))).collect(Collectors.toList());
        return CommonUtils.generateTreeMapCurrency(collect, "id", "parentId", "children");
    }

}
